﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
using ImageRecognition2.MouseImageTools;

namespace ImageRecognition2
{
    /// <summary>
    /// 
    /// </summary>
    public class ImageRecognition2Definitions
    {
        private static string m_sFileFilter
            = "bmp|dib|emf|gif|ico|j2k|jp2|jpf|jpg|jpm|jpx|pcx|nef|nrw|pdf|png|pbm|pgm|pnm|ppm|psd|rtf|tga|txt|svg|wmf";

        private static string m_sFilterTypeStringListFile = "FilterTypeListStringFilter.flt";

        private static string m_sConcreteFilter = string.Empty;
        
        private static Dictionary<string, ImageFormat> m_aImageFormat = new Dictionary<string, ImageFormat>();
        private static string m_sAutoGeneratedFileFilter = string.Empty;

        /// <summary>
        /// Initialize Definitions
        /// </summary>
        static ImageRecognition2Definitions()
        {
            m_sAutoGeneratedFileFilter = GetImageFilter();

            string sConcreteFileFilter = string.Empty;
            string[] aSplit = Regex.Split(m_sFileFilter, "\\|");
            foreach (string sFileFilter in aSplit)
            {
                string sFilter = string.Format("{0} (*.{0})|*.{0}", sFileFilter);
                sConcreteFileFilter += sFilter + "|";
            }
            sConcreteFileFilter += "All Files (*.*)|*.*";
            m_sConcreteFilter = sConcreteFileFilter;

            InitializeKnownImageFormats();
        }

        /// <summary>
        /// Autogeneratedfilefilter
        /// </summary>
        public static string AutogeneratedFileFilter
        {
            get { return m_sAutoGeneratedFileFilter; }
        }

        /// <summary>
        /// Gets image formats
        /// </summary>
        public static Dictionary<string, ImageFormat> KnownImageFormats
        {
            get { return m_aImageFormat; }
        }

        //TODO: add those from LowLevelGraphics.ImageFormats
        /// <summary>
        /// Initializes known image formats
        /// </summary>
        private static void InitializeKnownImageFormats()
        {
            m_aImageFormat.Add("bmp", ImageFormat.Bmp);
            m_aImageFormat.Add("emf", ImageFormat.Emf);
            m_aImageFormat.Add("gif", ImageFormat.Gif);
            m_aImageFormat.Add("ico", ImageFormat.Icon);
            m_aImageFormat.Add("jpg", ImageFormat.Jpeg);
            m_aImageFormat.Add("png", ImageFormat.Png);
            m_aImageFormat.Add("tif", ImageFormat.Tiff);
            m_aImageFormat.Add("tiff", ImageFormat.Tiff);
            m_aImageFormat.Add("wmf", ImageFormat.Wmf);
        }

        /// <summary>
        /// Get the Filter string for all supported image types.
        /// This can be used directly to the FileDialog class Filter Property.
        /// </summary>
        /// <returns></returns>
        private static string GetImageFilter()
        {
            StringBuilder allImageExtensions = new StringBuilder();
            string separator = "";
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            Dictionary<string, string> images = new Dictionary<string, string>();
            foreach (ImageCodecInfo codec in codecs)
            {
                allImageExtensions.Append(separator);
                allImageExtensions.Append(codec.FilenameExtension);
                separator = ";";
                images.Add(string.Format("{0} Files: ({1})", codec.FormatDescription, codec.FilenameExtension),
                           codec.FilenameExtension);
            }
            StringBuilder sb = new StringBuilder();
            if (allImageExtensions.Length > 0)
            {
                sb.AppendFormat("{0}|{1}", "All Images", allImageExtensions.ToString());
            }
            images.Add("All Files", "*.*");
            foreach (KeyValuePair<string, string> image in images)
            {
                sb.AppendFormat("|{0}|{1}", image.Key, image.Value);
            }
            return sb.ToString();
        }

        /// <summary>
        /// Gets or sets the file filter.
        /// </summary>
        /// <value>The file filter.</value>
        public static string FileFilter
        {
            get { return m_sConcreteFilter; }
            internal set
            {
            }
        }

        /// <summary>
        /// Filter Type String List File
        /// </summary>
        public static string FilterTypeStringListFile
        {
            get { return m_sFilterTypeStringListFile; }
        }
    }
}
